<template>
  <div>
    <el-form :model="searchList" ref="searchList">
      <el-row :gutter="20">
        <el-col :span="5" :offset="0">
          <el-form-item label="">
            <el-input v-model="searchList.project_name" placeholder="项目名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="0">
          <el-form-item label="">
            <el-date-picker v-model="searchList.create_time" type="date" value-format="yyyy-MM-dd" placeholder="建成时间"
              :clearable="true" class="wid">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="0">
          <el-form-item label="">
            <el-select v-model="searchList.project_model" value-key="project_model" placeholder="建设模式" clearable>
              <el-option v-for="item in d_buildType" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="9" :offset="0">
          <div class="right">
            <el-button type="primary" size="medium" @click="search" :loading="loading">查询</el-button>
            <el-button type="primary" size="medium" @click="importProject" :loading="loading">导入往年运维项目</el-button>
            <el-button type="primary" size="medium" @click="addProject" :loading="loading">添加项目</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div>共 {{titleData.project}}
      个项目，申请金额合计：{{titleData.apply_amount?titleData.apply_amount.toFixed(4):'0.0000'}}万元
    </div>
    <el-table :data="tableData" border stripe :height="offsetHeight" v-loading="loading">
      <el-table-column type="index" label="序号" width="50" align="center">
      </el-table-column>
      <el-table-column label="项目类型" width="80" align="center">
        <template slot-scope="{row}" v-if="row.copy_status">
          <el-tag type="success" size="medium" v-if="row.copy_status ==='0'">无变化</el-tag>
          <el-tag type="warning" size="medium" v-else-if="row.copy_status ==='1'">有变化</el-tag>
          <el-tag type="primary" size="medium" v-else>新增</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="project_name" label="项目名称" min-width="300" align="center">
      </el-table-column>
      <el-table-column prop="amount" label="总建设投资金额" width="100" align="center">
      </el-table-column>
      <el-table-column label="建设模式" width="100" align="center">
        <template slot-scope="{row}">
          {{row.project_model | f_buildType}}
        </template>
      </el-table-column>
      <el-table-column label="建成时间" width="100" align="center">
        <template slot-scope="{row}">
          {{$util.formatDate(row.create_time)}}
        </template>
      </el-table-column>
      <el-table-column prop="maintenance_amount" label="以往运维资金" width="100" align="center">
      </el-table-column>
      <el-table-column prop="service_pre_amount_sum" label="申请金额" width="100" align="center">
      </el-table-column>
      <el-table-column label="操作" width="400" fixed="right" align="center">
        <template slot-scope="{row}">
          <el-button-group>
            <el-button type="primary" size="mini" @click="projectInfo(row)">项目信息</el-button>
            <el-button type="primary" size="mini" @click="contractInfo(row)">合同信息</el-button>
            <el-button type="warning" size="mini" @click="changeStatus(row.id)" v-if="row.copy_status=='0'">
              变更状态
            </el-button>
            <el-button type="success" size="mini" @click="submit(row)">上报</el-button>
            <el-button type="danger" size="mini" @click="remove(row.id)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <!-- 往年项目 -->
    <el-drawer title="往年项目" :visible.sync="pastShow" size="70%" center :destroy-on-close="true"
      :wrapperClosable="false">
      <past-project ref="pastProject"></past-project>
      <div class="center">
        <el-button type="danger" @click="copyCancel" :loading="saveLoading">取消</el-button>
        <el-button type="primary" @click="copyProject" :loading="saveLoading">复制为无变化项目</el-button>
        <el-button type="warning" @click="copyProject1" :loading="saveLoading">复制为有变化项目</el-button>
      </div>
    </el-drawer>
    <!-- 项目信息 -->
    <el-drawer title="项目信息" :visible.sync="projectShow" size="70%" center :destroy-on-close="true"
      :wrapperClosable="false" :before-close="cancel">
      <div class="container">
        <el-alert title="无变化项目不可重新修改" type="info" effect="light" :closable="false" show-icon></el-alert>
      </div>
      <project-item :project-params="projectParams" :switch="this.switch" :copy-status="copyStatus" @cancel="cancel"
        @init="init"></project-item>
    </el-drawer>

  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import ProjectItem from '@/components/Project'
import PastProject from './past-project'
import { d_buildType } from '@/utils/dicts.js'
import { f_buildType } from '@/utils/filters.js'

export default {
  name: 'NoFinish',
  components: {
    PastProject, ProjectItem
  },
  props: {
    switch: {
      type: Boolean,
      default: () => {
        return true
      }
    },
  },
  data () {
    return {
      offsetHeight: document.documentElement.clientHeight - 280,
      searchList: {},
      loading: false,
      saveLoading: false,
      tableData: [],
      titleData: {},
      pastShow: false,
      projectShow: false,
      projectParams: {},
      d_buildType: d_buildType,
      copyStatus: '3',
    }
  },
  created () {

  },
  mounted () {

  },
  methods: {
    async init () {
      const res = await this.$http('findWholeList', {
        status: '0',
        check_status: '1',
      })
      this.titleData = res
      this.loading = true
      const res1 = await this.$http('inputProjectList', {
        check_status: '1',
        del_flag: '0',
        status: '0',
        menu_level: '1',
        ...this.searchList
      })
      this.tableData = res1
      this.loading = false
    },
    search () {
      this.init()
    },
    importProject () {
      this.pastShow = true
    },
    cancel () {
      this.projectShow = false
      this.projectParams = {}
      this.copyStatus = '3'
    },
    addProject () {
      this.projectShow = true
      this.projectParams = {}
    },
    projectInfo (item) {
      this.projectShow = true
      this.projectParams = {
        pid: item.id,
      }
      this.copyStatus = item.copy_status
    },
    contractInfo (item) {
      sessionStorage.setItem("inputInfo",
        JSON.stringify({
          pid: item.id,
          pcode: item.project_code,
          pname: item.project_name,
          copy_status: item.copy_status
        })
      );
      this.$emit('toContract')
    },
    submit (item) {
      this.$confirm('是否确认上报?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submit1(item)
      }).catch(() => {

      });
    },
    async submit1 (item) {
      // 获取系统时间
      let check_date = ''
      let date = new Date()
      let today = this.$util.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      check_date = await this.$http('checkDate', {
        today: today,
        num: '1'
      })
      // 更新状态
      this.loading = true
      const res = await this.$http('inputProjectUp', {
        id: item.id,
        company_code: item.company_code,
        check_status: '1',
        status: '1',
        check_end_time: check_date,
        log: {
          project_id: item.id,
          company_code: item.company_code,
          login_name: this.user.loginName,
          check_status: '1',
          status: '0',
          target_status: '1',
        }
      })
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }
      this.init()
    },
    remove (id) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        ids.push(id)
        this.remove1(ids)
      }).catch(() => {

      });
    },
    async remove1 (ids) {
      const res = await this.$http('inputDel', {
        level: '0',
        ids: ids,
        del_flag: '1'
      })
      this.$tips.success(res.errmsg)
      this.init()
    },
    changeStatus (id) {
      this.$confirm('是否确认变更状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changeStatus1(id)
      }).catch(() => {
      });
    },
    async changeStatus1 (id) {
      this.loading = true
      const res = await this.$http('inputProjectUp', {
        id: id,
        copy_status: '1'
      })
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }

      this.init()
    },
    // 项目复制
    copyCancel () {
      this.saveLoading = false
      this.pastShow = false
      this.$refs.pastProject.copyCancel()
    },
    async copyProject () {
      this.saveLoading = true
      const list = this.$refs.pastProject.copy()
      if (!this.copyCheck(list)) {
        this.saveLoading = false
        return false
      }
      let ids = []
      list.forEach(item => {
        ids.push(item.projectId)
      })
      const res = await this.$http('historyAdd', {
        ids: ids,
        copy_status: '0'
      })
      this.$tips.success(res.errmsg)
      this.saveLoading = false
      this.pastShow = false
      this.init()
    },
    async copyProject1 () {
      this.saveLoading = true
      const list = this.$refs.pastProject.copy()
      if (!this.copyCheck(list)) {
        this.saveLoading = false
        return false
      }
      let ids = []
      list.forEach(item => {
        ids.push(item.projectId)
      })
      const res = await this.$http('historyAdd', {
        ids: ids,
        copy_status: '1'
      })
      this.$tips.success(res.errmsg)
      this.saveLoading = false
      this.pastShow = false
      this.init()
    },
    copyCheck (list) {
      if (list.length === 0) {
        this.$tips.error('请选项复制项目')
        return false
      }
      if (list.length > 3) {
        this.$tips.error('一次最多只能选中三个项目进行复制')
        return false
      }
      return true
    },
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  filters: {
    f_buildType: f_buildType
  }
}
</script>
<style scoped>
</style>
